<?php

/**
 * tache form.
 *
 * @package    gesp
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class tacheForm extends BasetacheForm {
	public function configure() {
		$this->widgetSchema->setNameFormat('tache[%s]');

		unset($this['created_at']);
		unset($this['updated_at']);
		unset($this['tac_etat']);
		unset($this['tac_termine']);
		unset($this['tac_charge_consommee']);
		unset($this['tac_charge_restante']);
		unset($this['tac_date_fin']);
		
		// affectation auto du projet en cours
		$this->setWidget('tac_prj_id', new sfWidgetFormInputHidden());
		$this->setWidget('tac_parent_id', new sfWidgetFormInputHidden());
		// $this->setValidator('tac_prj_id', new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('projet'))));

		if (!$this->getObject()->isFils()) {
			$q = Doctrine_Query::Create()
				->from('categorie')
				->where('cat_prj_id = ?', $this->getObject()->getTacPrjId());
			$this->setWidget(
				'tac_cat_id', 
				new sfWidgetFormDoctrineChoice(array(
					'model' => $this->getRelatedModelName('categorie'), 
					'query' => $q, 
					'order_by' => array('cat_categorie', 'asc'), 
					'add_empty' => '-- select --',
				))
			);
		} else {
			$this->setWidget('tac_cat_id', new sfWidgetFormInputHidden());
		}
		
		// affectation auto de la date de début, modifiable
		$this->setWidget('tac_date_debut', new DatePickerWidget(array('default' => date('d/m/Y'))));
		$this->setWidget('tac_date_echeance', new DatePickerWidget());
		
		// redéfinition du widget urgence
		$this->setWidget(
			'tac_urgence', 
			new sfWidgetFormChoice(array(
				// 'choices'  => Doctrine_Core::getTable('libelle')->getUrgenceList(),
				'choices'  => LibelleTable::getUrgenceList(),
				'default' => 1,
				'expanded' => true,
			))
		);

		// redéfinition des validateurs
		$this->validatorSchema['tac_libelle']->setOption('required', true);
		$this->setValidator(
			'tac_date_debut',
			new ValidatorDate(array(
				'required' => true,
				'date_format' => '/^(?P<day>\d+)\/(?P<month>\d+)(\/(?P<year>\d+))?/',
				),
				array(
				'bad_format' => "format de date invalide : dd/mm[/yyyy]",
				)
			)
		);
		$this->setValidator(
			'tac_date_echeance',
			new ValidatorDate(array(
				'required' => false,
				'date_format' => '/^(?P<day>\d+)\/(?P<month>\d+)(\/(?P<year>\d+))?/',
				),
				array(
				'bad_format' => "format de date invalide : dd/mm[/yyyy]",
				)
			)
		);

		if ($this->getObject()->isFils()) {
			$date_debut_pere = $this->getObject()->getTachePere()->getTacDateDebut();
			$date_echeance_pere = $this->getObject()->getTachePere()->getTacDateEcheance();
			if ($date_debut_pere == '') {
				$date_debut_pere = '1900-01-01';
			}
			if ($date_echeance_pere == '') {
				$date_echeance_pere = '2099-12-31';
			}
			
			$this->validatorSchema['tac_date_debut']->setOption('min', $date_debut_pere);
			$this->validatorSchema['tac_date_debut']->setOption('max', $date_echeance_pere);
			$this->validatorSchema['tac_date_debut']->setMessage('min', "La date doit être postérieure à $date_debut_pere");
			$this->validatorSchema['tac_date_debut']->setMessage('max', "La date doit être antérieure à $date_echeance_pere");
			$this->validatorSchema['tac_date_echeance']->setOption('min', $date_debut_pere);
			$this->validatorSchema['tac_date_echeance']->setOption('max', $date_echeance_pere);
			$this->validatorSchema['tac_date_echeance']->setMessage('min', "La date doit être postérieure à $date_debut_pere");
			$this->validatorSchema['tac_date_echeance']->setMessage('max', "La date doit être antérieure à $date_echeance_pere");
		}

		
		// modification des libellés
		$this->widgetSchema->setLabels(array(
			'tac_cat_id' => 'Catégorie',
			'tac_libelle' => 'Tâche',
			'tac_description' => 'Description',
			'tac_urgence' => 'Urgence',
			'tac_date_debut' => 'Date de début',
			'tac_date_echeance' => 'Date d\'échéance',
			'tac_charge_estimee' => 'Charge estimée',
		));
		
		// modification des messages d'erreur
		$this->validatorSchema['tac_cat_id']->setMessage('required', 'Champ requis');
		$this->validatorSchema['tac_libelle']->setMessage('required', 'Champ requis');
		
		// Ajouter un contrôle de la charge / date échéance
	}
	
	public function bind(array $taintedValues = null, array $taintedFiles = null) {
		// $taintedValues['tac_prj_id'] = sfContext::getInstance()->getUser()->getAttribute('prj_id');
		return parent::bind($taintedValues, $taintedFiles);
	}
}
